/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserLogin.java
 *
 * Created on Dec 22, 2013, 6:08:17 PM
 */
package userLogin;

import common.ConnectionFactory;
import common.StringUtility;
import componentUI.LimitCharacterDocument;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HoangHai
 */
public class UserLogin extends javax.swing.JFrame {

    private static UserLogin userLogin;
    private String QUERY_SELECT_TANTOU = "SELECT TANTOU_ID, TAN_NAME, TAN_PWORD, START_DATE, END_DATE, TAN_CHANGE_PWDATE FROM TANTOU_MST"
            + " WHERE 1 = 1"
            + " AND START_DATE <= CURRENT_DATE"
            + " AND (END_DATE >= CURRENT_DATE OR END_DATE IS NULL)"
            + " AND TAN_NAME = ? AND TAN_PWORD = ?";

    /** Creates new form UserLogin */
    private UserLogin() {
        initComponents();
        this.getRootPane().setDefaultButton(btnLogin);
        setLocationRelativeTo(null);
    }

    public static synchronized UserLogin getMainMenuView() {
        if (userLogin == null) {
            userLogin = new UserLogin();
        }
        return userLogin;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblUsername = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        btnClear = new javax.swing.JButton();
        btnLogin = new javax.swing.JButton();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Công ty cổ phần thiết bị Đông Đô");
        setResizable(false);

        lblUsername.setText("Tên đăng nhập");

        lblPassword.setText("Mất khẩu");

        btnClear.setText("Xóa nhập");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        btnLogin.setText("Đăng nhập");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        txtUsername.setDocument(new LimitCharacterDocument(12));

        txtPassword.setDocument(new LimitCharacterDocument(12));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUsername)
                            .addComponent(lblPassword))
                        .addGap(71, 71, 71)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(btnLogin)
                        .addGap(68, 68, 68)
                        .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(214, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPassword)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLogin)
                    .addComponent(btnClear))
                .addContainerGap(114, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        String userName = txtUsername.getText().trim();
        char[] pass = txtPassword.getPassword();
        String password = null;
        if (pass != null && pass.length > 0) {
            password = new String(pass);
            for (int i = 0; i < 3; i++) {
                password = StringUtility.encoderByMd5(password);
            }
        }
        if (!StringUtility.isNull(userName) && !StringUtility.isNull(password)) {
            Connection connection = ConnectionFactory.createMySQLConnection();
            PreparedStatement stm = null;
            ResultSet rs = null;

            if (connection != null) {
                try {
                    stm = connection.prepareStatement(QUERY_SELECT_TANTOU);
                    if (stm != null) {
                        stm.setString(1, userName);
                        stm.setString(2, password);
                        rs = stm.executeQuery();
                        boolean hasNext = rs.next();
                        System.out.println(hasNext);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(UserLogin.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    ConnectionFactory.closeResultSet(rs);
                    ConnectionFactory.closeStatement(stm);
                    ConnectionFactory.closeConnection(connection);
                }
            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        txtPassword.setText(null);
        txtUsername.setText(null);
    }//GEN-LAST:event_btnClearActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
